package com.shine.manager
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.FullScreenEvent;
	import flash.geom.Rectangle;
	import flash.system.Capabilities;
	
	[Event(name="fullScreenOn",type="com.shine.manager.ScreenManager")]
	[Event(name="fullScreenOff",type="com.shine.manager.ScreenManager")]
	
	public class StageManager
	{
		//event
		public static const FULL_SCREEN_ON:String="fullScreenOn";
		public static const FULL_SCREEN_OFF:String="fullScreenOff";
		
		private static var eventDispatcher:EventDispatcher=new EventDispatcher();
		private static var stage:Stage;
		
		private static var INITIAL_STAGE_WIDTH:int;
	    private static var INITIAL_STAGE_HEIGHT:int;
		
		public static function initStage(theStage:Stage):void
		{
			if(stage==null)
			{
				stage=theStage;
				
				INITIAL_STAGE_WIDTH = stage.stageWidth;
		        INITIAL_STAGE_HEIGHT = stage.stageHeight;
				
				stage.addEventListener(FullScreenEvent.FULL_SCREEN,onFullscreen);
			}
		}
		
		public static function get SCREEN_WIDTH():Number
		{
			return Capabilities.screenResolutionX;
		}
		
		public static function get SCREEN_HEIGHT():Number
		{
			return Capabilities.screenResolutionY;
		}
		
		public static function getStage():Stage
		{
			if(stage==null)
			{
				return null;
			}
			return stage;
		}
		
		public static function getStageWidth():Number
		{
			return stage.stageWidth;
		}
		
		public static function getStageHeight():Number
		{
			return stage.stageHeight;
		}
		
		public static function turnOnFullScreen():void
		{
			stage.displayState="fullScreen"; 
		}
		
		public static function turnOffFullScreen():void
		{
			stage.displayState="normal";
		}
		
		public static function toggleFullScreen():void
		{
			switch(stage.displayState){
				case "normal":
					stage.displayState="fullScreen";
					break;
				case "fullScreen":
					stage.displayState="normal";
					break;
			}
		}
		
		private static function onFullscreen(evt:FullScreenEvent):void
		{
			switch(stage.displayState)
			{
				case "normal":
					eventDispatcher.dispatchEvent(new Event(FULL_SCREEN_OFF));
					break;
				case "fullScreen":
					eventDispatcher.dispatchEvent(new Event(FULL_SCREEN_ON));
					break;
			}
		}
		
		public static function addEventListener(type:String,listener:Function):void
		{
			eventDispatcher.addEventListener(type,listener);
		}
		
		public static function removeEventListener(type:String,listener:Function):void
		{
			if(eventDispatcher.hasEventListener(type)){
				eventDispatcher.removeEventListener(type,listener);
			}
		}
		
		public static function disableDefaultFocusRect():void
		{
			stage.stageFocusRect=false;
		}
		
		public static function getInitialStageSize():Rectangle
	    {
	    	if(stage == null){
	    		throw new Error("the stage must be initialized in the document class!");
	    	}
	    	return new Rectangle(0,0,INITIAL_STAGE_WIDTH, INITIAL_STAGE_HEIGHT);
	    }
	    
	    /**
	     * Returns the stage current size.
	     * @return the size. 
	     */
	    public static function getStageSize():Rectangle
	    {
	    	if(stage == null){
	    		throw new Error("the stage must be initialized in the document class!");
	    	}
	    	return new Rectangle(0,0,stage.stageWidth, stage.stageHeight);
	    }
		
		/**
		 * Returns whether or not stage is set to the manager.
		 * @return whether or not stage is set to the manager.
		 */
		public static function isStageInited():Boolean
		{
			return stage != null;
		}
	}
}